class LocationsController < ApplicationController
  before_filter :except => [:show, :outgoing] do |controller|
    controller.authorize_permissions({:blocked_permissions => (User::PermissionLevels - ["admin"])})
  end
  
  # GET /locations
  # GET /locations.json
  def index
    @title = "View Locations"
	
    session[:show_hidden_items] ||= false
    @show_hidden = session[:show_hidden_items]
    
    if @show_hidden
      @locations = Location.all
    else
      @locations = Location.find_all_by_hidden false
    end
		
    respond_to do |format|
      format.html # index.html.erb
      format.json  { render :json => @locations }
    end
  end
  
  # GET /locations/1
  # GET /locations/1.json
  def show
    @location = Location.find(params[:id])
    @title = "View Location: " + @location.name

    respond_to do |format|
      format.html # show.html.erb
      format.json  { render :json => [@location] }
    end
  end

  # GET /locations/new
  # GET /locations/new.json
  def new
    @title = "New Location"
    @location = Location.new

    respond_to do |format|
      format.html # new.html.erb
      format.json  { render :json => @location }
    end
  end

  # GET /locations/1/edit
  def edit
    @location = Location.find(params[:id])
    @title = "Edit Location: " + @location.name
  end

  # POST /locations
  # POST /locations.json
  def create
    @location = Location.new(params[:location])

    respond_to do |format|
      if @location.save
        flash[:notice] = 'Location was successfully created.'
        format.html { redirect_to :action => "index" }
        format.json  { render :json => @location, :status => :created, :location => @location }
      else
		@title = "New Location"
        format.html { render :action => "new" }
        format.json  { render :json => @location.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /locations/1
  # PUT /locations/1.json
  def update
    @location = Location.find(params[:id])

    respond_to do |format|
      if @location.update_attributes(params[:location])
        flash[:notice] = 'Location was successfully updated.'
        format.html { redirect_to :action => "index" }
        format.json  { head :ok }
      else
		@title = "Edit Location: " + @location.name
        format.html { render :action => "edit" }
        format.json  { render :json => @location.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  def outgoing
    @items = Item.find_all_by_source_id_and_delivered(params[:id], false)
    respond_to do |format|
        format.json  { render :json => @items }
    end
  end

  # DELETE /locations/1
  # DELETE /locations/1.json
  def destroy
    @location = Location.find(params[:id])
    @location.hidden = !@location.hidden
	
    if @location.save
      flash[:notice] = @location.hidden ? 'Location hidden' : 'Location un-hidden'
    else
      flash[:notice] = @location.hidden ? "Location couldn't be hidden." : "Location couldn't be un-hidden"
    end
	
    respond_to do |format|
      format.html { redirect_to(locations_url) }
      format.json  { head :ok }
    end
  end
  
  def toggle_hidden
    session[:show_hidden_items] ||= false
    session[:show_hidden_items] = !session[:show_hidden_items]
	
    redirect_to :back
  end
end
